﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml.Linq;
using LogLib;
using System.IO;

namespace EECMISWS
{
    public class CountryReader
    {
        public string AssemblyDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        private string _error = "";
        public string Error
        {
            get { return _error; }
        }

        private Dictionary<string, string> _mapCountryCode = new Dictionary<string, string>();
        public Dictionary<string, string> CountryCodes
        {
            get { return _mapCountryCode; }
        }

        private Dictionary<string, List<CountryCodeMap>> _mapCountryIndexs = new Dictionary<string, List<CountryCodeMap>>();
        public Dictionary<string, List<CountryCodeMap>> CountryIndexs
        {
            get { return _mapCountryIndexs; }
        }

        private List<List<string>> listRowIndexs = new List<List<string>>();
        public List<List<string>> RowIndexs
        {
            get { return listRowIndexs; }
        }

        public CountryReader()
        {
           
        }

        public bool OnReading()
        {
            string file = Path.Combine(AssemblyDirectory, "DICTable", "QWD.xml");
            _error = "";
            XDocument xmlDoc;
            try
            {
                xmlDoc = XDocument.Load(file);
            }
            catch (Exception ex)
            {
                _error = "国家/地区代码配置文件加载发生了异常:" + ex.Message;
                Log.WriteException(ex.ToString());
                return false;
            }
            if (xmlDoc == null)
            {
                _error = "加载配置文件失败.";
                Log.WriteAsync(_error);
                return false;
            }
            XElement root = xmlDoc.Element("root");
            if (root == null)
            {
                _error = "找不到名为root的根节点.";
                Log.WriteAsync(_error);
                return false;
            }
            foreach(XElement e in root.Elements())
            {
                string rowIndex = e.Attribute("row").Value;
                string indexName = e.Attribute("name").Value;
                List<CountryCodeMap> listCode = new List<CountryCodeMap>();
                foreach (XElement country in e.Elements())
                {
                    string name = country.Attribute("name").Value;
                    string code = country.Attribute("code").Value;
                    listCode.Add(new CountryCodeMap(name, code));
                    _mapCountryCode.Add(name, code);
                }
                _mapCountryIndexs.Add(indexName, listCode);
                AddRowIndex(indexName, rowIndex);
            }          
            return true;
        }

        private void AddRowIndex(string name, string index)
        {
            if(string.IsNullOrEmpty(index))
            {
                throw new Exception("国家/地区名称索引为空!");
            }
            int i = 0;
            if(!int.TryParse(index, out i))
            {
                throw new Exception("国家/地区名称索引无法转换为有效的索引!");
            }
            if(i > listRowIndexs.Count)
            {
                listRowIndexs.Add(new List<string>());
            }
            listRowIndexs[i-1].Add(name);
        }
    }
}
